package com.ailot.cloud.base.security.authentication.handler;

import cn.hutool.http.HttpStatus;
import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.base.security.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未授权访问处理
 */
@Slf4j
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        log.error(e.getMessage());
        Result<String> result = Result.fail(e.getMessage());
        httpServletResponse.setStatus(HttpStatus.HTTP_FORBIDDEN);
        ResponseUtil.response(httpServletResponse, result);
    }
}
